package com.renhui.android.handwriting.handler;

public class Handler {

    private Looper looper;
    private MessageQueue messageQueue;

    public Handler() {
        looper = Looper.myLooper();
        if (looper == null) {
            throw new RuntimeException("Can't create handler inside thread " + Thread.currentThread()
                    + ", that has not called Looper.prepare()");
        }
        messageQueue = looper.mQueue;
    }

    public void handlerMessage(Message msg) {

    }

    public void sendMessage(Message msg) {
        // 将消息放入消息队列MessageQueue中
        enqueueMessage(msg);
    }

    private void enqueueMessage(Message msg) {
        // 赋值当前消息处理对象
        msg.target = this;
        // 使用MessageQueue，将消息传入
        messageQueue.enqueueMessage(msg);
    }

    public void dispatchMessage(Message msg) {
        handlerMessage(msg);
    }
}
